<template>
  <div>
    <div class="comment-form">
      <el-form :model="commentAdd.postData" :rules="commentAdd.rules" ref="commentForm">
        <el-form-item style="margin-right: 0" label-width="0px" label="" prop="content">
          <emoticon-input
            :placeholder="placeholder"
            v-model="commentAdd.postData.content"
            :emoticonClass="emoticonClass"
            @send="send"
          ></emoticon-input>
        </el-form-item>

        <div class="row">
          <div class="inner-row">
            <el-form-item label-width="" label="" prop="gender">
              <select-gender v-model="commentAdd.postData.gender"></select-gender>
            </el-form-item>
          </div>
          <div class="inner-row">
            <el-form-item label-width="" label="" prop="nickname">
              <el-input size="medium" v-model="commentAdd.postData.nickname" placeholder="称呼..."></el-input>
            </el-form-item>
            <el-form-item label-width="" label="" prop="email">
              <el-input
                size="medium"
                v-model="commentAdd.postData.email"
                placeholder="QQ邮箱 (回复后,会发送QQ邮箱提示~)"
              ></el-input>
            </el-form-item>
          </div>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import selectGender from '../select-gender/select-gender';
import emoticonInput from '../emoticon-input/emoticon-input';

export default {
  props: {
    emoticonClass: {
      default: '',
    },
    placeholder: {
      default: '来都来了,说俩句呗~ ',
    },
  },
  name: 'WorkspaceJsonCommentForm',
  components: {
    selectGender,
    emoticonInput,
  },
  data() {
    return {
      commentAdd: {
        postData: {
          gender: '',
          content: '',
          nickname: '',
          email: '',
          headImg: '',
        },
        rules: {
          content: [{ required: true, message: '空空如也~', trigger: 'blur' }],
          nickname: [{ required: true, message: '怎么称呼呢', trigger: 'blur' }],
          gender: [{ required: true, message: '是其中一个不', trigger: 'blur' }],
          email: [
            {
              required: true,
              message: '填个邮箱呗,回复后系统会邮箱给通知',
              trigger: 'blur',
            },
          ],
        },
      },
    };
  },

  mounted() {
    let userInfo = this.$store.state.userInfo;
    this.commentAdd.postData.email = userInfo.email;
    this.commentAdd.postData.gender = userInfo.gender;
    this.commentAdd.postData.nickname = userInfo.nickname;
    this.commentAdd.postData.headImg = userInfo.headImg;
  },
  methods: {
    send() {
      this.$refs['commentForm'].validate(valid => {
        this.editUserInfo();
        if (valid) {
          this.$emit('send', this.commentAdd.postData, this.clearData);
        }
      });
    },
    clearData() {
      this.commentAdd.postData.content = '';
    },
    editUserInfo() {
      let qq = this.commentAdd.postData.email.split('@')[0];
      if (qq) {
        this.commentAdd.postData.headImg = `https://q2.qlogo.cn/headimg_dl?dst_uin=${qq}&spec=640`;
      }
      this.$http('editUserInfo', this.commentAdd.postData).then(res => {
        this.$store.commit('setUserInfo', {
          ...this.$store.state.userInfo,
          ...this.commentAdd.postData,
        });
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.comment-form {
  margin-bottom: 10px;
  .head {
    width: 30px;
    height: 30px;
    background: #f5f5f5;
    line-height: 30px;
    text-align: center;
    color: #999;
    border-radius: 2px;
    margin-top: 1px;
    border: 1px solid #ccc;
    margin-right: 10px;
  }
  .el-form-item {
    margin-bottom: 0px;
  }

  .row {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    margin-top: 5px;
    .inner-row {
      display: flex;
      &:nth-child(1) {
        margin: 5px 0;
      }
      &:nth-child(2) {
        flex: 1;
        flex-wrap: wrap;
        .el-form-item {
          flex: 1;
          min-width: 170px;
          margin-bottom: 0;
          margin-right: 10px;
        }
      }
    }
  }
}
</style>
